﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tetris
{
    public class Piece
    {
        public Directions Direction;
        public List<Square> squares;
        public bool isStopped;
        public bool onLeftBorder, onRightBorder;
        public Piece previousPiece;


        public Piece(List<Square> squares)
        {
            this.squares = squares;
        }

        public virtual void SetColors() { }
        public virtual void SetPieceLocationsForHoldPiece() { }
        public virtual void SetPieceLocationsForGrid(){}

        public virtual void RotateRight()
        {
            Direction = (Directions)Direction+1;
            onLeftBorder = false;
            onRightBorder = false;
            int previousGridX, previousGridY;

            previousPiece = this;

            Square pivotSquare = squares.Where(x => x.isPivot == true).FirstOrDefault();

            foreach (Square square in squares)
            {
                if (!square.isPivot)
                {
                    //Store previous in case of invalid rotate
                    previousGridX = square.gridX;
                    previousGridY = square.gridY;

                    //Set x and y values in relation to pivot square
                    square.gridX -= pivotSquare.gridX;
                    square.gridY -= pivotSquare.gridY;

                    //Rotate
                    int tempX = square.gridX;
                    square.gridX = square.gridY * -1;
                    square.gridY = tempX;

                    //Add back pivot square relational values for final location on grid
                    square.gridX += pivotSquare.gridX;
                    square.gridY += pivotSquare.gridY;
                }
            }
            CheckForValidRotation();
        }

        protected void CheckForValidRotation()
        {
            bool outOfLeftBounds = true;
            while (outOfLeftBounds)
            {
                outOfLeftBounds = false;
                foreach (Square square in squares)
                {
                    if (square.gridX < 0)
                        outOfLeftBounds = true;
                }
                if (outOfLeftBounds)
                {
                    foreach (Square square in squares)
                    {
                        square.gridX++;
                    }
                }
            }

            bool outOfRightBounds = true;
            while (outOfRightBounds)
            {
                outOfRightBounds = false;
                foreach (Square square in squares)
                {
                    if (square.gridX > 9)
                        outOfRightBounds = true;
                }
                if (outOfRightBounds)
                {
                    foreach (Square square in squares)
                    {
                        square.gridX--;
                    }
                }
            }
        }

        public Piece GetPreviousPiece()
        {
            return previousPiece;
        }

        public void MoveLeft()
        {
            onLeftBorder = false;
            foreach (Square square in squares)
            {
                if (square.gridX == 0)
                    onLeftBorder = true;
            }
            if (!onLeftBorder && !isStopped)
            {
                foreach (Square square in squares)
                {
                    if (square.gridX > 0)
                        square.gridX--;
                }
            }
        }

        public void MoveRight()
        {
            onRightBorder = false;
            foreach (Square square in squares)
            {
                if (square.gridX == 9)
                    onRightBorder = true;
            }
            if (!onRightBorder && !isStopped)
            {
                foreach (Square square in squares)
                {
                    if (square.gridX < 9)
                        square.gridX++;
                }
            }
        }

        public void MoveDown()
        {
            Square squareToRemove = null;
            foreach (Square square in squares)
            {
                if (square.isEmpty)
                    squareToRemove = square;
                if (square.gridY == 21)
                    isStopped = true;
            }
            if (squareToRemove != null)
                squares.Remove(squareToRemove);
            if (isStopped)
            {
                foreach (Square square in squares)
                {
                    square.isStopped = true;
                }
            }
            if (!isStopped)
            {
                foreach (Square square in squares)
                {
                    square.gridY++;
                }
            }
        }
    }
}
